<template>
  <div class="about">
    <el-button @click="show">发布文章</el-button>
    <div style="float: right; margin-right: 60px; width: 500px">
      <el-input v-model="obj.title" placeholder="请输入查询文章标题"></el-input>
    </div>
    <!-- 文章列表 -->
    <!-- {{this.art}} -->
    <!-- <el-table :data="tableData" style="width: 100%"> -->
    <el-table class="main_table" :data="this.art.list" style="width: 100%">
      <el-table-column prop="id" label="编号" width="80"></el-table-column>
      <el-table-column prop="title" label="文章标题" width="500"></el-table-column>
      <!-- <el-table-column prop="content" label="内容">
        <template v-slot="scope">
          <div v-html="scope.row.content"></div>
        </template>
      </el-table-column> -->

      <!-- 时间方法1 -->
      <el-table-column prop="publishTime" label="发布时间" width="200">
        <template v-slot="scope">
          <!-- |管道符  将左侧的变量做为参数传递够右侧的函数 -->
          <div>{{ scope.row.publishTime | dateFMT }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="category.name" label="栏目" width="180"></el-table-column>
      <!-- 时间方法2 -->
      <!-- <el-table-column prop="publishTime" :formatter='fmt' label="发布时间">
      </el-table-column>-->

      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button @click="review_comment(scope.row)">查看详情</el-button>
          <el-button @click="delete_article(scope.row.id)">删除</el-button>
          <el-button @click="edit_article(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="obj.pageSize"
      :current-page="obj.page"
      :total="this.total"
      @current-change="change"
    ></el-pagination>
    <!-- {{obj}} -->
    <!-- 模态框 -->
    <div class="atc">
      <!-- <img alt="Vue logo" src="../assets/logo.png">
      <HelloWorld msg="Welcome to Your Vue.js App"/>-->
      <!-- <el-button @click="show">登录</el-button> -->
      <el-dialog :title="title1" :visible.sync="dialog_article">
        <el-form :model="form_article">
          <el-form-item label="文章标题" :label-width="formLabelWidth_article">
            <el-input v-model="form_article.title"></el-input>
          </el-form-item>
          <el-form-item label="文章内容" :label-width="formLabelWidth_article">
            <!-- <el-input v-model="form_article.content" type="textarea" :rows="2" placeholder="请输入内容"></el-input> -->
            <Tinymce v-model="form_article.content"></Tinymce>
          </el-form-item>
          <el-form-item label="文章类型" :label-width="formLabelWidth_article">
            <el-select v-model="form_article.categoryId" placeholder="请选择">
              <el-option
                v-for="item in category"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- {{options}} -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="delete_article">取 消</el-button>
          <el-button type="primary" @click="saveOrupdate">保存</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<style>
  .main_table{
    font-size: 16px;
  }
</style>

<script>
// 不是全局
import axios from 'axios'
import qs from 'qs'
import moment from 'moment'
// import { mapActions } from 'vuex';
import { mapState, mapActions } from 'vuex'
// 导入组件
import Tinymce from '../../components/Tinymce'

export default {
  components: {
    // 注册组件
    Tinymce
  },
  data() {
    return {
      title1: '',
      obj: {
        page: 1,
        pageSize: 10
      },
      //   tableData: [],
      total: 0,
      dialog_article: false,
      form_article: {},
      formLabelWidth_article: '120px'
      //   options: []
    }
  },

  computed: {
    ...mapState('article', ['art', 'category'])
  },
  // 监听器
  // 监听变量变化时，执行监听中的代码
  watch: {
    obj: {
      handler: function() {
        this.page_query_article()
      },
      deep: true
    }
  },
  created() {
    // 不是全局
    // console.log(qs);
    // 全局
    // console.log(this.qs);

    // if (localStorage.getItem("token")) {
    //   this.page_query_article();
    // }
    this.page_query_article()
    this.find_category()
  },
  // 过滤器--处理日期--方法1
  filters: {
    dateFMT(str) {
      var res = moment(str).format('YYYY-MM-DD HH:mm:ss')
      // console.log(str);
      return res
    }
  },
  methods: {
    ...mapActions('article', ['findAllCategory', 'page_query_art']),
    // 修改按钮
    edit_article(val) {
      // console.log(val)
      // 弹出模态框
      this.dialog_article = true
      // 放数据---先序列化为json对象 在反序列化成对象--改变对象指针---防止还没有点确认就改变标题
      this.form_article = JSON.parse(JSON.stringify(val))
      // this.form_article=val;

      // 标题
      this.title1 = '修改文章'
    },
    // 查询所有栏目
    find_category() {
      this.findAllCategory()
      //   this.axios({
      //     url: "http://39.102.37.84:8001/category/findAll",
      //     methond: "get"
      //   }).then(res => {
      //     // console.log(res.data.data);
      //     this.options = res.data.data;
      //     // console.log(this.options);
      //   });
      // this.options = this.category;
      // console.log(this.category);
    },

    // 显示模块框发布文章
    show() {
      this.dialog_article = true
      this.title1 = '发布文章'
    },
    // 保存文章
    saveOrupdate() {
      axios({
        url: 'http://39.102.37.84:8001/article/saveOrUpdate',
        method: 'post',
        data: qs.stringify(this.form_article)
      }).then(res => {
        // 关闭模态框
        this.dialog_article = false
        // 刷新数据
        this.page_query_article()
        // 清空模态框
        this.form_article = {}
        this.form_article.content = {}
      })
    },
    review_comment(art) {
      this.$router.push({
        path: "/manager/comment",
        query: {art:JSON.stringify(art)},
      });
    },
    // 处理日期--方法2-用表格自带发方法
    fmt(row, column) {
      return moment(row.publishTime).format('YYY-MM-DD HH:mm:ss')
    },

    // 页码改变

    change(page) {
      this.obj.page = page
    },
    // // 删除文章
    delete_article(id) {
      axios({
        url: 'http://39.102.37.84:8001/article/deleteById',
        method: 'get',
        params: { id }
      }).then(res => {
        this.page_query_article()
      })
    },
    // 分页查询文章
    page_query_article() {
      this.page_query_art(this.obj).then(()=>{
        this.total = this.art.total
      })
      // this.tableData =this.art.list;
      // console.log(this.total)
    }
  }
}
</script>
  